package com.miracle.main;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.miracle.basemvp.base.BaseActivity;
import com.miracle.basemvp.di.component.AppComponent;
import com.miracle.common.listener.ViewPagerListener;
import com.miracle.main.adapter.HomePageAdapter;
import com.miracle.main.module.mvp.ui.fragment.FileFragment;
import com.miracle.main.module.mvp.ui.fragment.SecretFragment;
import com.miracle.main.module.mvp.ui.fragment.SettingFragment;
import com.trello.rxlifecycle2.LifecycleProvider;
import com.trello.rxlifecycle2.android.ActivityEvent;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import me.majiajie.pagerbottomtabstrip.Controller;
import me.majiajie.pagerbottomtabstrip.PagerBottomTabLayout;
import me.majiajie.pagerbottomtabstrip.listener.OnTabItemSelectListener;

/**
 * @author WangJQ
 */
@Route(path = "/main/MainActivity")
public class MainActivity extends BaseActivity implements OnTabItemSelectListener{

    @BindView(R2.id.main_pager)
    ViewPager mViewPager;
    @BindView(R2.id.main_tab)
    PagerBottomTabLayout mBottomTabLayout;

    private int mBottomColor;

    private Controller mController;

    private HomePageAdapter mPagerAdapter;
    private List<Fragment> mFragments;


    @Override
    public int setLayoutId(@Nullable Bundle bundle) {
        return R.layout.cloud_activity_main;
    }

    @Override
    public void initView() {
        initTab();
        initFragment();
    }

    private void initTab() {
        mBottomColor = ContextCompat.getColor(mContext,R.color.colorPrimary);
        mController = mBottomTabLayout.builder()
                .addTabItem(R.drawable.icon_message_2, "秘密", mBottomColor)
                .addTabItem(R.drawable.icon_file_2, "文件", mBottomColor)
                .addTabItem(R.drawable.icon_setting_1, "设置", mBottomColor)
                .build();
        mController.addTabItemClickListener(this);
        mController.setSelect(1);
    }

    private void initFragment(){
        mFragments = new ArrayList<>();
        mFragments.add(new SecretFragment());
        mFragments.add(new FileFragment());
        mFragments.add(new SettingFragment());
        mPagerAdapter = new HomePageAdapter(getSupportFragmentManager(), mFragments);
        mViewPager.setAdapter(mPagerAdapter);
        mViewPager.addOnPageChangeListener(new ViewPagerListener(){
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                mController.setSelect(position);
            }
        });
    }

    @Override
    public void initData() {

    }

    @Override
    public void setDaggerComponent(AppComponent appComponent) {

    }

    @Override
    public void onRequest(LifecycleProvider<ActivityEvent> lifecycleProvider) {

    }

    @Override
    public void onSelected(int index, Object tag) {
        mViewPager.setCurrentItem(index);
    }

    @Override
    public void onRepeatClick(int index, Object tag) {

    }
}
